package cn.itcast.bos.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> {

	private static final long serialVersionUID = 1L;
	// 使用模型驱动：BaseAcrion<Area> 代替
	// private Standard standard = new Standard();
	// @Override
	// public Standard getModel() {
	// // TODO Auto-generated method stub
	// return standard;
	// }

	@Autowired
	private StandardService standardService;

	/**
	 * 保存Standard
	 */
	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
	public String save() {
		standardService.save(model);
		return SUCCESS;
	}

	/**
	 * 异步校验：Standard是否已存在（name）
	 */
	@Action(value = "standard_validateName", results = { @Result(name = "success", type = "json") })
	public String validateName() {
		// 传递页面传递在name值
		List<Standard> list = standardService.findByName(model.getName());
		if (list != null && list.size() == 0) {
			// 数据库中不存在记录，可以使用
			ServletActionContext.getContext().getValueStack().push(true);
		} else {
			// 数据库中存在记录，不可以使用
			ServletActionContext.getContext().getValueStack().push(false);
		}

		return SUCCESS;
	}

	/*
	 * 无条件分页查询所有
	 */
	private int page;// 当前页
	private int rows;// 每页记录数

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "standard_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 组织分页数据
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Standard> page = standardService.findPageData(pageable);

		// 返回客户端数据 需要 total 和 rows
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());

		// 将map转换为json数据返回 ，使用struts2-json-plugin 插件
		ServletActionContext.getContext().getValueStack().push(map);

		return SUCCESS;
	}

	/**
	 * 删除Standard
	 */
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "standard_deleteStandard", results = {
			@Result(name = "success", location = "./pages/base/standard.html", type = "redirect") })
	public String deleteStandard() {
		System.out.println("ids=" + ids);
		// 按,分隔ids
		String[] idArray = ids.split(",");
		System.out.println("idArray=" + idArray);
		// 调用业务层，批量作废
		standardService.deleteStandard(idArray);
		return SUCCESS;
	}

	/**
	 * 收派标准回显
	 */
	@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
	public String findAll() {
		List<Standard> list = standardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

}
